#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
using pii = pair<int, int>;
#define x first
#define y second
const int N = 1010, M = N * N;
char g[N][N];
bool st[N][N];
pii q[M];
int n, m;
void bfs(int a, int b)
{
    int hh = 0, tt = 0;
    q[0] = { a,b };
    while (hh <= tt)
    {
        int sx = q[hh].x;
        int sy = q[hh++].y;
        for (int i = sx - 1; i <= sx + 1; i++)
        {
            for (int j = sy - 1; j <= sy + 1; j++)
            {
                if (i == sx && j == sy) continue;
                if (i < 0 || i >= n || j < 0 || j >= m) continue;
                if (g[i][j] == '.' || st[i][j] == true) continue;
                q[++tt] = { i,j };
                st[i][j] = true;
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++) cin >> g[i][j];
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (g[i][j] == 'W' && st[i][j] == false) bfs(i, j), res++;
        }
    }
    cout << res << endl;
    return 0;
}